package com.dylankenny.flikrreader.android;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.dylankenny.flikrreader.R;

public class MainActivity extends Activity {

	private static final String URL_KEY = "URL_KEY";
	private HashMap<Button, String> buttonUrlMap;

	public static String getUrlKey() {
		return URL_KEY;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_main);

		this.initializeButtonUrls();
		if (this.buttonUrlMap != null)
			this.initializeButtonActions(this.buttonUrlMap);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		this.getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * Assign actions to the buttons, telling them to launch the feed activity
	 * 
	 * @author cathbar
	 * @param buttonMap
	 */
	private void initializeButtonActions(HashMap<Button, String> buttonMap) {
		for (Button button : buttonMap.keySet()) {
			final String url = buttonMap.get(button);
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent myIntent = new Intent(MainActivity.this, FeedActivity.class);
					myIntent.putExtra(URL_KEY, url);
					MainActivity.this.startActivity(myIntent);
				}
			});
		}
	}

	/**
	 * Map buttons to the urls
	 * 
	 * @author cathbar
	 */
	private void initializeButtonUrls() {
		Button jsonButton = (Button) this.findViewById(R.id.button_json);
		Button xmlButton = (Button) this.findViewById(R.id.button_xml);

		this.buttonUrlMap = new HashMap<Button, String>();
		// TODO put these in R.strings
		this.buttonUrlMap.put(jsonButton, this.getString(R.string.url_json));
		this.buttonUrlMap.put(xmlButton, this.getString(R.string.url_xml));
	}

}
